# encoding: utf-8

###########################################################################################################
#
#
# Trade Font Connecting broken strokes
#
#
###########################################################################################################

from __future__ import division, print_function, unicode_literals
import objc
from GlyphsApp import Glyphs
from GlyphsApp.plugins import FilterWithoutDialog


class Connectionbrokenstrokes(FilterWithoutDialog):
    @objc.python_method
    def settings(self):
        self.menuName = Glyphs.localize({
            'zh': '贸易体连接断开笔画',
        })

    @objc.python_method
    def filter(self, Layer, inEditView, customParameters):
        # Apply your filter code here
        s_ds = []
        trans_ = 68
        p_layer = 0
        for c in range(len(Layer.paths)):
            for psd in Layer.paths[c].nodes:
                if psd.selected == True:
                    s_p = []
                    s_p.append(psd.x)
                    s_p.append(psd.y)
                    s_ds.append(s_p)
                    p_layer = c
        if len(s_ds) == 1:
            Glyphs.showNotification('Export fonts', 'select one point!')
            all_nds = []
            for spsd in Layer.paths[p_layer].nodes:
                a = []
                a.append(spsd.x)
                a.append(spsd.y)
                all_nds.append(a)
        elif len(s_ds) == 2:
            all_nds = []
            for spsd in Layer.paths[p_layer].nodes:
                a = []
                a.append(spsd.x)
                a.append(spsd.y)
                all_nds.append(a)
        if len(all_nds) == 4 and len(s_ds) == 2:
            result_sort_ = sorted(all_nds, key=lambda p: (-p[1], p[0]))
            a = [result_sort_[0], result_sort_[1]]
            b = [result_sort_[2], result_sort_[3]]
            sorted_a = sorted(a, key=lambda point: point[0])
            sorted_b = sorted(b, key=lambda point: point[0], reverse=True)
            result_sort_ = sorted_a + sorted_b
            if (s_ds[0] == result_sort_[0] and s_ds[1] == result_sort_[3]) or (
                    s_ds[0] == result_sort_[3] and s_ds[1] == result_sort_[0]):
                k = (result_sort_[3][1] - result_sort_[2][1]) / (result_sort_[3][0] - result_sort_[2][0])
                for spsd in Layer.paths[p_layer].nodes:
                    if spsd.selected == True:
                        spsd.y -= k * trans_
                        spsd.x -= trans_
            elif (s_ds[0] == result_sort_[0] and s_ds[1] == result_sort_[1]) or (
                    s_ds[0] == result_sort_[1] and s_ds[1] == result_sort_[0]):
                k = (result_sort_[0][1] - result_sort_[3][1]) / (result_sort_[0][0] - result_sort_[3][0])
                for spsd in Layer.paths[p_layer].nodes:
                    if spsd.selected == True:
                        spsd.x += trans_ / k
                        spsd.y += trans_
            elif (s_ds[0] == result_sort_[1] and s_ds[1] == result_sort_[2]) or (
                    s_ds[0] == result_sort_[2] and s_ds[1] == result_sort_[1]):
                k = (result_sort_[1][1] - result_sort_[0][1]) / (result_sort_[1][0] - result_sort_[0][0])
                for spsd in Layer.paths[p_layer].nodes:
                    if spsd.selected == True:
                        spsd.y += k * trans_
                        spsd.x += trans_
            elif (s_ds[0] == result_sort_[2] and s_ds[1] == result_sort_[3]) or (
                    s_ds[0] == result_sort_[3] and s_ds[1] == result_sort_[2]):
                k = round((result_sort_[0][1] - result_sort_[3][1]) / (result_sort_[0][0] - result_sort_[3][0]), 2)
                # print(k)
                for spsd in Layer.paths[p_layer].nodes:
                    if spsd.selected == True:
                        spsd.x -= trans_ / k
                        spsd.y -= trans_
            Layer.clearSelection()
        elif len(all_nds) == 8 and len(s_ds) == 2:
            max_x = max(x for x, y in all_nds)
            max_y = max(y for x, y in all_nds)
            min_x = min(x for x, y in all_nds)
            min_y = min(y for x, y in all_nds)
            cpx = round((max_x - min_x) / 2, 0) + min_x
            cpy = round((max_y - min_y) / 2, 0) + min_y
            cpy_2 = max_y - round((max_y - min_y) / 2, 0)
            # put in x z
            if s_ds[0][0] <= cpx and s_ds[1][0] <= cpx:
                for spsd in Layer.paths[p_layer].nodes:
                    if spsd.selected == True:
                        spsd.x -= trans_
            elif s_ds[0][0] >= cpx and s_ds[1][0] >= cpx:
                for spsd in Layer.paths[p_layer].nodes:
                    if spsd.selected == True:
                        spsd.x += trans_
            Layer.clearSelection()
        elif len(all_nds) == 6 and len(s_ds) == 2:
            max_x = max(x for x, y in all_nds)
            max_y = max(y for x, y in all_nds)
            min_x = min(x for x, y in all_nds)
            min_y = min(y for x, y in all_nds)
            cpx = round((max_x - min_x) / 2, 0) + min_x
            cpy = round((max_y - min_y) / 2, 0) + min_y
            cpy_2 = max_y - round((max_y - min_y) / 2, 0)
            if s_ds[0][0] >= cpx and s_ds[1][0] >= cpx:
                for spsd in Layer.paths[p_layer].nodes:
                    if spsd.selected == True:
                        spsd.x += trans_
            elif s_ds[0][0] <= cpx and s_ds[1][0] <= cpx:
                for spsd in Layer.paths[p_layer].nodes:
                    if spsd.selected == True:
                        spsd.x -= trans_
            Layer.clearSelection()
        elif len(all_nds) >= 10 and len(s_ds) == 2:
            o_x = Layer.paths[p_layer].bounds.origin.x
            o_y = Layer.paths[p_layer].bounds.origin.y
            o_w_c = round(Layer.paths[p_layer].bounds.size.width / 2, 0)
            o_h_c = round(Layer.paths[p_layer].bounds.size.height / 2, 0)
            c_x = o_x + o_w_c
            c_y = o_y + o_h_c
            # print(c_x)
            # print(c_y)
            if (s_ds[0][0] >= c_x and s_ds[1][0] >= c_x) or (s_ds[0][0] > c_x - 50 and s_ds[1][0] > c_x - 50):
                for spsd in Layer.paths[p_layer].nodes:
                    if spsd.selected == True:
                        spsd.x += trans_
            elif s_ds[0][0] <= c_x and s_ds[1][0] <= c_x:
                for spsd in Layer.paths[p_layer].nodes:
                    if spsd.selected == True:
                        spsd.x -= trans_
            Layer.clearSelection()
        elif len(all_nds) == 4 and len(s_ds) == 1:  # input point
            # sorted
            result_sort_ = sorted(all_nds, key=lambda p: (-p[1], p[0]))
            a = [result_sort_[0], result_sort_[1]]
            b = [result_sort_[2], result_sort_[3]]
            sorted_a = sorted(a, key=lambda point: point[0])
            sorted_b = sorted(b, key=lambda point: point[0], reverse=True)
            result_sort_ = sorted_a + sorted_b
            # print(s_ds[0][0], result_sort_[2][0], s_ds[0][1], result_sort_[2][1])
            if s_ds[0][0] == result_sort_[2][0] and s_ds[0][1] == result_sort_[2][1]:
                k = round((result_sort_[1][1] - result_sort_[2][1]) / (result_sort_[1][0] - result_sort_[2][0]), 10)
                for spsd in Layer.paths[p_layer].nodes:
                    if spsd.selected == True:
                        output = 1 + (1 / 3)
                        pathTime = spsd.index + output
                        Layer.paths[p_layer].insertNodeWithPathTime_(pathTime)
                infl = 0
                for spsd in Layer.paths[p_layer].nodes:
                    if spsd.selected ==True:
                        infl += 1
                        if infl == 1:
                            Layer.paths[p_layer].insertNodeWithPathTime_(spsd.index + 0.5)
                        else:
                            break
                for spsd in Layer.paths[p_layer].nodes:
                    if spsd.selected == True:
                        pre_nd = spsd.prevNode
                        nxt_nd = spsd.nextNode
                        pre_x_ = spsd.x - spsd.prevNode.x
                        nxt_x_ = nxt_nd.x - spsd.x
                        nxt_y_ = nxt_nd.y - spsd.y
                        pre_nd.x += (pre_x_ + trans_)
                        spsd.x += (nxt_x_ + trans_)
                        spsd.y += nxt_y_
            Layer.clearSelection()
        else:
            Glyphs.showNotification('Export fonts', 'Error_2!!!')
            Layer.clearSelection()

    @objc.python_method
    def __file__(self):
        """Please leave this method unchanged"""
        return __file__
